package com.lfxfs;

import com.lfxfs.soap.request.soap.ReceB2COrderRequest;
import com.lfxfs.soap.request.soap.SoapBody;
import com.lfxfs.soap.request.soap.SoapEnvelope;
import com.lfxfs.soap.utils.JaxbBinder;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Arrays;

public class TestJaxb {

    public static void main(String[] args) {
        TestJaxb testJaxb = new TestJaxb();
        testJaxb.testToObject();
        testJaxb.testToXml();
    }

    public void testToObject() {
        JaxbBinder jaxbBinder = new JaxbBinder(SoapEnvelope.class);
        URL url = this.getClass().getClassLoader().getResource("receB2COrderResponse.xml");
        String xml = null;
        try {
            xml = FileUtils.readFileToString(new File(url.getPath()), Charset.defaultCharset());
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

        SoapEnvelope envelope = jaxbBinder.fromXml(xml);

        System.out.println(envelope);
        System.out.println(envelope.getBody());
        System.out.println(envelope.getBody().getReceB2COrderResponseList());
        System.out.println(envelope.getBody().getReceB2COrderResponseList().get(0));
        System.out.println(envelope.getBody().getReceB2COrderResponseList().get(0).getOrderNo());
        System.out.println("getOrderNo=" + envelope.getBody().getReceB2COrderResponseList().get(0).getOrderNo());

        System.out.println("\ndata=" + (envelope).toString());


    }

    public void testToXml() {
        ReceB2COrderRequest re = new ReceB2COrderRequest();
        re.setOrderNo("test111123123");
        re.setPageUrl("asdfsadfasdf");
        re.setCardValue("aaaaaaaaaaa");
        SoapBody body = new SoapBody();
        body.setReceB2COrderRequestList(Arrays.asList(re));
        SoapEnvelope envelope = new SoapEnvelope(null, body);
        JaxbBinder jaxbBinder = new JaxbBinder(SoapEnvelope.class);
        String xml = jaxbBinder.toXml(envelope, "utf-8");
        System.out.println("\nxml=" + xml);
    }
}
